package com.nbicc.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Shane
 * @date: 2017-10-19 上午9:57
 */
//@JsonInclude(JsonInclude.Include.NON_NULL)
//@ToString
public class ResponseObj {

    public static ResponseObj SUCCESS = new ResponseObj(Const.HTTP_STATUS_SUCCESS);
    public static ResponseObj FAIL = new ResponseObj(Const.HTTP_STATUS_FAIL);
    public static ResponseObj UNAUTHORIZED = new ResponseObj(Const.UNAUTHORIZED);

    private int status;
    private String error;
    private String message;
    private Map<String,Object> bodyMap;

    public static ResponseObj success(){
        return  new ResponseObj(Const.HTTP_STATUS_SUCCESS);
    }

    public static ResponseObj fail(){
        return  new ResponseObj(Const.HTTP_STATUS_FAIL);
    }

    public ResponseObj(int status){
        this.status = status;
    }

    public ResponseObj(int status,String error){
        this.status = status;
        this.error = error;
    }

    public ResponseObj(int status,String error,String message){
        this.status = status;
        this.error = error;
        this.message = message;
    }

    public ResponseObj(int status,Object result){
        this.status = status;
        getExistBodyMap().put("result",result);
    }

    public ResponseObj(int status,Object result,long total){
        this.status = status;
        getExistBodyMap().put("result",result);
        getExistBodyMap().put("total",total);
    }


    public int getStatus() {
        return status;
    }

    public ResponseObj setStatus(int status) {
        this.status = status;
        return this;
    }

    public String getError() {
        return error;
    }

    public ResponseObj setError(String error) {
        this.error = error;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ResponseObj setMessage(String message) {
        this.message = message;
        return this;
    }

    public Map<String, Object> getBodyMap() {
        return bodyMap;
    }

    public ResponseObj setBodyMap(Map<String, Object> bodyMap) {
        this.bodyMap = bodyMap;
        return this;
    }

    public ResponseObj setResult(Object result){
        getExistBodyMap().put("result",result);
        return this;
    }

    private Map<String,Object> getExistBodyMap(){
        if(this.bodyMap == null){
            this.bodyMap = new HashMap<String,Object>();
        }
        return this.bodyMap;
    }

    @JsonIgnore
    public Object getResult(){
        return getExistBodyMap().get("result");
    }

}
